package com.luojun.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import com.luojun.entity.Car;
import com.luojun.entity.Commodity;
import com.luojun.util.HibernateUtil;


public class CommodityDao {
	Session session = null;
	/**
	 * 添加方法
	 * @param car
	 */
	public void save(Commodity commodity){
		session = HibernateUtil.getCurrentSession();
		session.save(commodity);
	}
	/**
	 * 删除方法
	 * @param ID
	 */
	public void del(int ID){
		session = HibernateUtil.getCurrentSession();
		Commodity commodity = session.get(Commodity.class,ID);
		session.delete(commodity);
	}
	/**
	 * 修改方法
	 * @param quantity
	 * @param ID
	 */
	public void update(Commodity commodity){
		session = HibernateUtil.getCurrentSession();
		session.update(commodity);
	}
	/**
	 * 根据关键字查询
	 * @param name
	 * @return
	 */
	public List<Commodity> findByname(String name){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.name like %?%");
		query.setString(0, name);
		List<Commodity> Clist=query.list();
		return Clist;
	}
	/**
	 * 全部查询
	 * @return
	 */
	public List<Commodity> findbyStatus(int status){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.status = ?");
		query.setInteger(0, status);
		List<Commodity> Clist=query.list();
		return Clist;
	}

	/**
	 * 根据userid查询
	 * @param userId
	 * @return
	 */
	public List<Commodity> findByUserId(int userId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.userId = ?");
		query.setInteger(0, userId);
		List<Commodity> Clist=query.list();
		return Clist;
	}
	/**
	 *  id  +  status 查询
	 * @param userid
	 * @param status
	 * @return
	 */
	public List<Commodity> findStatusAndUser(int userid , int status){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.status = ? and c.user = ? ");

		query.setInteger(0, status);
		query.setInteger(1, userid);
		List<Commodity> Clist=query.list();
		return Clist;
	}
	
	/**
	 * 总数据数
	 * @return
	 */
	public int findCountbyUseid(int status ,int userid){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("select count(*) from Commodity c where c.status=? and c.user = ? ");
		//Query query= session.createQuery("select count(*) from Commodity where status=1");
		query.setInteger(0, status);
		query.setInteger(1, userid);
		long count = (long)query.uniqueResult();
		return (int)count;
	}
	/**
	 * 根据commodityId查询
	 * @param commodityId
	 * @return
	 */
	public List<Commodity> findByCommodityId(int commodityId){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.commodityId = ?");
		query.setInteger(0, commodityId);
		List<Commodity> Clist=query.list();
		return Clist;
	}
	
	/**
	 * 总数据数
	 * @return
	 */
	public int findCount(int status){
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("select count(*) from Commodity c where c.status=? ");
		//Query query= session.createQuery("select count(*) from Commodity where status=1");
		query.setInteger(0, status);
		long count = (long)query.uniqueResult();
		return (int)count;
	}
	
	/**
	 * 分页查询
	 * @param nowpageNum
	 * @param pageSize
	 * @return
	 */
	public List<Commodity> findByPage(int nowpageNum,int pageSize,int status) {
		session = HibernateUtil.getCurrentSession();
		Query query= session.createQuery("from Commodity c where c.status=? order by c.commodityId desc");
		query.setFirstResult((nowpageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		query.setInteger(0, status);
		List<Commodity> Clist=query.list();
		return Clist;
	}
	
	/**
	 * 总数据数
	 * @return
	 */
	public int findCountbyName(int status , String name){
		session = HibernateUtil.getCurrentSession();
		String hql = "select count(*) from Commodity c where c.status=? and c.name like '%"+name+"%'";
		Query query= session.createQuery(hql);
		//Query query= session.createQuery("select count(*) from Commodity where status=1");
		System.out.println(name);
		query.setInteger(0, status);
//		query.setString("name", "'%"+name+"%'");
		long count = (long)query.uniqueResult();
		return (int)count;
	}
	
	/**
	 * 分页查询
	 * @param nowpageNum
	 * @param pageSize
	 * @return
	 */
	public List<Commodity> findByPageName(int nowpageNum,int pageSize,int status,String name) {
		session = HibernateUtil.getCurrentSession();
		String hql ="from Commodity c where c.status=? and c.name like '%"+name+"%' order by c.commodityId desc";
		Query query= session.createQuery(hql);
		query.setFirstResult((nowpageNum-1)*pageSize);
		query.setMaxResults(pageSize);
		query.setInteger(0, status);
//		query.setString("name", "'%"+name+"%'");
		List<Commodity> Clist=query.list();
		return Clist;
	}
}
